package com.ayoka.ui.assignment;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.widget.RadioGroup;

import com.ayoka.R;
import com.ayoka.adapter.CommonPagerAdapter;
import com.ayoka.base.AppContext;
import com.ayoka.base.BaseActivity;
import com.ayoka.base.BasePresenter;
import com.ayoka.bean.CourseAssignmentBean;
import com.ayoka.databinding.ActivityCourseAssignmentVideoDetailBinding;
import com.ayoka.mvp.CourseAssignmentMVP;
import com.ayoka.Constant;
import com.jcp.baselibrary.model.ToolBarOptions;
import com.jcp.baselibrary.permission.MPermission;

import java.io.File;

/**
 * @author jcp
 * @ClassName: TopicListActivity
 * @Description:
 * @date 2018-12-28 下午4:21:18
 */
public class CourseAssignmentVideoDetailActivity extends BaseActivity<ActivityCourseAssignmentVideoDetailBinding, BasePresenter>  {

    // 视频文件位置
    public static final String VIDEO_DIRECTORY = AppContext.getAppCacheDir() + File.separator + "video";

    public static void start(Activity activity, CourseAssignmentBean courseAssignmentBean) {
        Intent intent = new Intent(activity, CourseAssignmentVideoDetailActivity.class);
        intent.putExtra("CourseAssignmentBean", courseAssignmentBean);
        activity.startActivityForResult(intent, REQUEST_CODE_FOR_RESULT);
    }

    private CourseAssignmentBean courseAssignmentBean;

    private CommonPagerAdapter pageAdapter;

    // 添加记录的Id
    private int recordId;

    @Override
    protected void configIntentValue() {
        courseAssignmentBean = (CourseAssignmentBean) getIntent().getSerializableExtra("CourseAssignmentBean");
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_course_assignment_video_detail;
    }

    @Override
    protected void initViews() {
        mDataBinding.rgSwitchMode.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (checkedId == mDataBinding.rbVideo.getId()){
                    mDataBinding.viewPager.setCurrentItem(0);
                } else if (checkedId == mDataBinding.rbAudio.getId()){
                    mDataBinding.viewPager.setCurrentItem(1);
                }
            }
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        ToolBarOptions options = new ToolBarOptions("作业");
        setCustomToolBar(mDataBinding.customToolbar.toolbar, options);

        configFragment();
        requestBasicPermission();
    }


    @Override
    protected CourseAssignmentMVP.CourseAssignmentPresenter getPresenter() {
        return new CourseAssignmentMVP.CourseAssignmentPresenter();
    }

    private void configFragment() {
        pageAdapter = new CommonPagerAdapter(getSupportFragmentManager());
        CourseAssignmentVideoFragment videoTabFragment = CourseAssignmentVideoFragment.newInstance(courseAssignmentBean);
        pageAdapter.addFragment(videoTabFragment, "视频");
        mDataBinding.viewPager.setAdapter(pageAdapter);
    }

    /**
     * 基本权限管理
     */
    private final String[] BASIC_PERMISSIONS = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.RECORD_AUDIO
    };

    private void requestBasicPermission() {
        MPermission.with(this)
                .setRequestCode(Constant.BASIC_PERMISSION_REQUEST_CODE)
                .permissions(BASIC_PERMISSIONS)
                .request();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        MPermission.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }
}
